package com.ming_za.odupdaterserver.core.service.account;

import com.ming_za.odupdaterserver.core.sqlite.domain.AdminAccount;
import com.ming_za.odupdaterserver.core.sqlite.mapper.AdminAccountMapper;
import com.ming_za.odupdaterserver.core.utils.account.param.AccountPackage;
import com.ming_za.odupdaterserver.core.utils.account.api.AccountStorage;
import com.ming_za.odupdaterserver.core.utils.cache.MapInternalCache;
import com.ming_za.odupdaterserver.core.vo.VoUtils;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账号存储接口
 */
@Component
public class AccountSQL implements AccountStorage {

    private final VoUtils voUtils;

    private final MapInternalCache<Long, AdminAccount> adminAccountCache;

    public AccountSQL(VoUtils voUtils,AccountCacheOptions accountCacheOptions) {
        this.voUtils = voUtils;
        this.adminAccountCache = new MapInternalCache<>(accountCacheOptions);
    }

    @Override
    public void saveAccount(AccountPackage accountPackage) {
        AdminAccount adminAccount = voUtils.cover(accountPackage,new AdminAccount());
        adminAccountCache.putValue(adminAccount.getAccount(),adminAccount);
    }

    @Override
    public AccountPackage getAccountPackage(long account) {
        return voUtils.cover(adminAccountCache.getValue(account),new AccountPackage());
    }

    @Override
    public Map<Long, AccountPackage> getAllAccountMap() {
        Map<Long, AccountPackage> accountCache = new HashMap<>();
        Collection<AdminAccount> values = adminAccountCache.getCacheData().values();
        for (AdminAccount adminAccount : values) {
            accountCache.put(adminAccount.getAccount(),voUtils.cover(adminAccount,new AccountPackage()));
        }
        return accountCache;
    }

    @Override
    public List<AccountPackage> getAllAccountPackage() {
        return adminAccountCache.getCacheData().values().stream().map(v->voUtils.cover(v,new AccountPackage())).toList();
    }

    @Override
    public void deleteAccount(long account) {
        adminAccountCache.removeValue(account);
    }

    @Override
    public void setPassword(long account, String password) {
        AdminAccount value = adminAccountCache.getValue(account);
        if(value != null){
            value.setPassword(password);
            adminAccountCache.save(account);
        }
    }

    @Override
    public void setToken(long account, byte[] token) {
        AdminAccount value = adminAccountCache.getValue(account);
        if(value != null){
            value.setTokenKey(token);
            adminAccountCache.save(account);
        }
    }

    @Override
    public void setLevel(long account, int level) {
        AdminAccount value = adminAccountCache.getValue(account);
        if(value != null){
            value.setLevel(level);
            adminAccountCache.save(account);
        }
    }

    @Override
    public void saveLogin(long account, byte[] token, String loginIp, long loginTime) {
        AdminAccount value = adminAccountCache.getValue(account);
        if(value != null){
            value.setTokenKey(token);
            value.setLastIp(loginIp);
            value.setLastLogin(loginTime);
            adminAccountCache.save(account);
        }
    }

    @Override
    public void setLastIp(long account, String loginIp) {
        AdminAccount value = adminAccountCache.getValue(account);
        if(value != null){
            value.setLastIp(loginIp);
            adminAccountCache.save(account);
        }
    }
}
